/******************************************************************************
COPYRIGHT 2012   : ATECH
Project          : 
Source File Name : driverE2.c
Group            : SoftWare Team
Author           : 
Date First Issued: 09/21/2012
********************************Documentation**********************************
Purpose -  This file is 
********************************RevisionHistory********************************
_______________________________________________________________________________
Date : MM/DD/YY    Release        Changes Description                  Author
Date : 09/21/2012    1.0                                                 
   
******************************************************************************/


#define driverE2_c


/* #include */

#include "sys_micro.h"
#include "system.h"
//#include "sys_clock.h"
#include "driverE2.h"

/* #define */ 
#define EEPROM_BASE  		0x13
#define	EEPROM_SAVE			0x12
#define E2_TIMEMAX  		26		//26ms Max

/* typedef */

/* Variables */

/* eeprom variables */
#pragma DATA_SEG EE_DATA 
 
  UINT8 EE_ROM[EE_TOTAL_SIZE];
  
#pragma DATA_SEG DEFAULT 


/* static variables */
static StatusE2 statusE2 = E2_IDLE;


/* Function prototypes */


/* static function prototypes */





/*****************************************************************************/
/**
* Function Name: void iniE2PROM(void)
* Description:   
* 
* Param:         none
* Return:        none
* Author:  
******************************************************************************/ 
void iniE2PROM(void) 
{
    while (!FSTAT_CCIF) 
    {					
        /* wait for FTM reset to complete */
    }
    
    FCLKDIV = SYSTEM_BUS_CLOCK - 1;    
  
}

/*****************************************************************************/
/**
 * Function Name: void Erase_Sector_4Byte(UINT8 address)
 * Description:  erase a sector,4 bytes.
 *
 * Param:   
 * Return:  none
 * Author:  none
 *****************************************************************************/   
void Erase_Sector_4Byte(UINT8 address)
{
    while(FSTAT_CCIF == 0);	 //wait if command in progress
	
	  if (FSTAT_ACCERR)        // If EEPROM access error flag is set
		{
		    FSTAT_ACCERR = 1;    // Then clear it 
		}
  
	  if (FSTAT_FPVIOL)        // If EEPROM write error flag is set
		{
		    FSTAT_FPVIOL = 1;    // Then clear it 
		}

	  FCCOBIX = 0;
	  FCCOBHI = 0x12;
	  FCCOBLO = 0x00;
	
	  FCCOBIX++;
  	FCCOBHI = EEPROM_BASE;
	  FCCOBLO = (address);

  	FSTAT_CCIF = ON;

	  while(FSTAT_CCIF == 0);		//wait if command in progress
	
}

/*****************************************************************************/
/**
 * Function Name: void writeE2( UINT8 *pBuf, UINT8 addr, UINT8 nBytes,
 *                              UINT8 *pStatus )
 * Description:   
 *
 * Param:   none
 * Return:  none
 * Author:  YuChaochao
 *****************************************************************************/
void writeE2(UINT8 *pBuf, UINT8 addr, UINT8 nBytes, UINT8 *pStatus) 
{
    UINT8 EE_TMP[4];
	
	  if( (nBytes==0) || (nBytes>4) || (addr%4!=0))
	  {
	      *pStatus = E2_ERROR_ARG;
	      return;
	  }
	  
	  *pStatus	 = E2_CMD_STARTED;
	  //-------------------------------------------------------------------------
	  //Save X Byte EEPROM's data In Ram  
		EE_TMP[0] = *pBuf;
		EE_TMP[1] = *(pBuf+1);
		EE_TMP[2] = *(pBuf+2);
		EE_TMP[3] = *(pBuf+3);	

	  //-------------------------------------------------------------------------
	  Erase_Sector_4Byte(addr);
	  //-------------------------------------------------------------------------
	  while(FSTAT_CCIF == 0);		//wait if command in progress
	
	  if (FSTAT_ACCERR)        	// If EEPROM access error flag is set
		{
		    FSTAT_ACCERR = 1;     // Then clear it 
		}
  
	  if (FSTAT_FPVIOL)       	// If EEPROM write error flag is set
		{
		    FSTAT_FPVIOL = 1;     // Then clear it 
		}

	  FCCOBIX = 0;
	  FCCOBHI = 0x11; // program D FLASH cmd
	  FCCOBLO = 0x00;  // addr: 22--16
	
	  FCCOBIX++;
	  FCCOBHI = EEPROM_BASE; // addr: 15-8
	  FCCOBLO = addr;   // addr: 7-0
	
	  FCCOBIX++;
  	FCCOBHI = EE_TMP[0];  // data0
  	FCCOBLO = EE_TMP[1];	  // data1
	
	  FCCOBIX++;
  	FCCOBHI = EE_TMP[2];  // data 2
  	FCCOBLO = EE_TMP[3];	 // data 3

	  FSTAT_CCIF = ON;

	  while(FSTAT_CCIF == 0);		//wait if command in progress  
	  
	  *pStatus = E2_CMD_DONE;
  
}

/*****************************************************************************/
/**
* Function Name: void readE2(UINT8 *pBuf, UINT8 addr, UINT8 nBytes, UINT8 *pStatus)
* Description:	 Initialization of applications
*
* Param:	none
* Return:	none
* Author:	
*****************************************************************************/
void readE2(UINT8 *pBuf, UINT8 addr, UINT8 nBytes, UINT8 *pStatus)
{
    UINT8 tmp;
    
	  if( (nBytes < 1) || ((nBytes+addr)>EE_TOTAL_SIZE))
  	{
	      *pStatus = E2_ERROR_ARG;
		    return;
  	}
	
 	  for(tmp=0; tmp<nBytes; tmp++)
	  {
		    *(pBuf+tmp) = EE_ROM[addr+tmp];
	  }
	  
  	*pStatus = E2_CMD_DONE;
  	
}  
  
/**********************end of driverE2.c**************************************/ 


/******************************************************************************
 Revision Notes:

For each change to this file, be sure to record:
1. Who made the change and when the change was made.
2. Why the change was made and the intended result.

  Date     By     Reason For Change
mm/dd/yy  XXX     -----------------

******************************************************************************/
